﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShopPowerTemplate.Common
{
    public class LiveConnectUserProfile
    {
        public string ID { get; private set; }
        public string Name { get; private set; }
        public string FirstName { get; private set; }
        public string LastName { get; private set; }
        public string Gender { get; private set; }
        public string Locale { get; private set; }
        public Dictionary<string, string> Emails { get; private set; }
        public string AccessToken { get; set; }

        public string AccountEmail
        {
            get
            {
                return Emails["account"];
            }
        }

        public LiveConnectUserProfile(IDictionary<string, object> dictionary)
        {
            // Common data
            ID = dictionary["id"] as string;
            Name = dictionary["name"] as string;
            FirstName = dictionary["first_name"] as string;
            LastName = dictionary["last_name"] as string;
            Gender = dictionary["gender"] as string;
            Locale = dictionary["locale"] as string;

            // Emails
            GetEmails(dictionary["emails"] as IDictionary<string, object>);
        }

        private void GetEmails(IDictionary<string, object> dictionary)
        {
            Emails = new Dictionary<string, string>();

            Emails["preferred"] = dictionary["preferred"] as string;
            Emails["account"] = dictionary["account"] as string;
            Emails["personal"] = dictionary["personal"] as string;
            Emails["business"] = dictionary["business"] as string;
        }

        public override string ToString()
        {
            if (!string.IsNullOrEmpty(FirstName) && !string.IsNullOrEmpty(LastName))
            {
                return string.Format("{0} {1}", FirstName, LastName);
            }

            return AccountEmail;
        }
    }
}
